#coding=utf-8
from time import sleep, strftime, localtime
import sdsource.web.sina as sina

def now():  
    return strftime('%H:%M:%S', localtime())
    
def rp2list(rp):
    r = [rp.time, rp.now, rp.change, rp.highest, rp.lowest, \
         rp.volume, rp.amount, rp.bid_buy, rp.bid_sell]
    
    b = []
    for i in rp.slot_buy:
        b.extend(i)
    r.extend(b)
    
    s = []  
    for i in rp.slot_sell:
        s.extend(i)
    r.extend(s)
   
    return r

if __name__ == "__main__":
    code = '600797'
    fn = '/home/jay/%s.csv'%code
    
    f = open(fn, 'w')
    while True:
        n = now()
        
        if n < '09:29:00' or \
           (n > '11:31:00' and n < '12:59:00') or \
           n > '15:01:00':
           break
        
        rp = sina.get_rtprice(code)
        rp_list = [ str(i) for i in rp2list(rp) ]
        
        f.write( ','.join(rp_list) )
        f.write('\n')
        f.flush()
        
        sleep(5)

    f.close()

    


